/* 
 * File:   SAllocator.h
 * Author: aishwarya
 *
 * Created on March 16, 2011, 2:25 PM
 * Interface for an abstraction to memory pool to avoid repeated malloc/free
 * calls in application.
 */

#ifndef SAllocator_H
#define	SAllocator_H

#include "avdtech.h"

#ifdef	__cplusplus
extern "C" {
#endif

    typedef struct _SAllocator {
        char *buf; /* memory pool */
        size_t e_size; /* size of one memory block in memory pool */
        int nele; /* number of blocks of memory in buf[], each block of size e_size */
        int avail; /* free blocks in buf[]*/
        int *reuse; /* reuse free'd blocks */
        int *inuse; /* same memory not free'd twice */
        int t ;
    } SAllocator ;

    /* Allocates entries*e_size bytes and initializes instance variables */
    int alloc_init(SAllocator *m, int entries, size_t e_size);

    /* Rezies an SAllocator to a bigger memory pool */
    int alloc_resize(SAllocator *m, int new_entries);

    /* Return a block of memory of size e_size */
    void* alloc_get(SAllocator *m);

    /* Returns a previously Get() block of memory to free pool */
    int alloc_free(SAllocator *m, void *b);

    /* Get number of blocks allocated */
    int alloc_count(SAllocator *m);

    /* Get available free blocks */
    int alloc_avail(SAllocator *m);

    /* Clears the memory */
    int alloc_clean(SAllocator *m);

#ifdef	__cplusplus
}
#endif

#endif	/* SAllocator_H */

